import 'package:jiffy/jiffy.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_title_chart_page.dart';
import 'package:safetymonitor/app/components/custom_refresh.dart';
import 'package:safetymonitor/app/components/time/MultiWheelTimePickerTimePeriod.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/ext/ext.dart';
import 'package:safetymonitor/app/data/entities/home/monitor/person_monitoring_page_entity_entity.dart';
import 'package:safetymonitor/gen/assets.gen.dart';
import 'controller.dart';

/// 最新人员监测
class NewPersonnelMonitoringPage extends StatelessWidget {
  NewPersonnelMonitoringPage({super.key});

  final controller = Get.put(NewPersonnelMonitoringController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Column(
            children: [
              _selectTime(),
              Expanded(
                  child: CustomEasyRefreshList<PersonMonitoringPageEntityEntity>(
                key: controller.globalKey,
                showEmpty: false,
                padding: EdgeInsets.only(top: 0.w, right: 16.w, left: 16.w),
                emptyWidgetTwo: Center(
                    child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    SvgPicture.asset('assets/svg/zhiban_empty_bg.svg'),
                    Text(
                      '暂无内容',
                      style: TextStyle(
                        color: const Color(0xFFC7CEE5),
                        fontSize: 14.sp,
                      ),
                    ),
                    AppGaps.vGap60,
                    AppGaps.vGap10,
                  ],
                )),
                dataLoad: controller.theFeignEmergencyPlansPage,
                itemBuilder: (item, index) {
                  return _itemWidget(item);
                },
              ))
            ],
          );
        });
  }

  Widget _itemWidget(PersonMonitoringPageEntityEntity item) {
    return Stack(
      children: [
        CustomTitleChartPage(
          contentPadding: EdgeInsets.only(right: 16.w, left: 16.w, top: 16.w),
          title: item.companyName ?? '',
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  Expanded(child: _getTextView(title: '人员姓名:', value: item.name ?? '')),
                  Expanded(child: _getTextView(title: '人员工种:', value: item.craftStr ?? '')),
                ],
              ),
              AppGaps.vGap6,
              _getTextView(title: '人员分组:', value: item.groupId ?? ''),
              AppGaps.vGap6,
              _getTextView(
                title: '进场时间:',
                value: int.parse(item.enterTime ?? '0') != 0 ? Jiffy.parseFromMillisecondsSinceEpoch(int.parse(item.enterTime ?? '0')).format(pattern: 'yyyy-MM-dd HH:mm:ss') : '',
              ),
              AppGaps.vGap6,
              _getTextView(
                title: '出场时间:',
                value: int.parse(item.exitTime ?? '0') != 0 ? Jiffy.parseFromMillisecondsSinceEpoch(int.parse(item.exitTime ?? '0')).format(pattern: 'yyyy-MM-dd HH:mm:ss') : '',
              )
            ],
          ),
        ),
        Positioned(
          right: 0,
          bottom: 0,
          child: SizedBox(
            width: 56.w,
            height: 36.w,
            child: Stack(
              alignment: Alignment.center,
              children: [
                SvgPicture.asset(
                  Assets.svg.iconEnter,
                  fit: BoxFit.cover,
                ),
                Icon(
                  Icons.arrow_forward,
                  size: 20.w,
                  color: Colors.white,
                ).marginOnly(left: 7.w, top: 6.w)
              ],
            ),
          ),
        )
      ],
    ).marginOnly(bottom: 10.w).clickOnce(() {
      controller.onClickDetail(item);
    });
  }

  /// 获取字段显示
  Widget _getTextView({required String title, required String value}) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              title,
              style: TextStyle(
                fontSize: 14.sp,
                height: 1.2,
                fontWeight: FontWeight.w400,
                color: const Color(0xffC7CEE5),
              ),
            ),
            AppGaps.hGap6,
            Expanded(
                child: Text(
              value,
              style: TextStyle(
                fontSize: 14.sp,
                height: 1.2,
                fontWeight: FontWeight.w400,
                color: const Color(0xff1D3452),
              ),
            ))
          ],
        ),
        AppGaps.vGap10,
      ],
    );
  }

  Widget _selectTime() {
    return Container(
      margin: EdgeInsets.only(left: 16.w, right: 16.w, top: 0.w),
      padding: EdgeInsets.only(left: 12.w, right: 12.w, top: 6.w, bottom: 6.w),
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(2.w),
          border: Border.all(
            color: AppColors.font_color_gray.withOpacity(0.3),
            width: 1,
          )),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text('今日进场人数：${controller.mTotal}人',
              style: TextStyle(
                fontSize: 15.sp,
              )),
          AppGaps.vGap2,
          Row(
            children: [
              SvgPicture.asset('assets/svg/icon_calendar.svg'),
              Expanded(
                  child: Text(
                controller.startDate,
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: 14.sp,
                  fontWeight: FontWeight.w400,
                  color: controller.startDate == '开始时间' ? const Color(0xffA0A7B5) : const Color(0xff666666),
                ),
              ).clickOnce((){
                    MultiWheelTimePickerTimePeriod.show(
                      initialDate: controller.startDate,
                      pattern: 'yyyy-MM-dd HH:mm',
                      onConfirm: (selectedDate) {
                        controller.startDate = selectedDate;
                        controller.refreshPage();
                      },
                    );
                  })),
              AppGaps.hGap5,
              Text(
                '至',
                style: TextStyle(
                  fontSize: 14.sp,
                  fontWeight: FontWeight.w400,
                  color: AppColors.font_color_black,
                ),
              ),
              AppGaps.hGap5,
              Expanded(
                  child: Text(
                controller.endDate,
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: 14.sp,
                  fontWeight: FontWeight.w400,
                  color: controller.endDate == '结束时间' ? const Color(0xffA0A7B5) : const Color(0xff666666),
                ),
              ).clickOnce((){
                    MultiWheelTimePickerTimePeriod.show(
                      initialDate: controller.endDate,
                      pattern: 'yyyy-MM-dd HH:mm',
                      onConfirm: (selectedDate) {
                        controller.endDate = selectedDate;
                        controller.refreshPage();
                      },
                    );
                  })),
            ],
          )
        ],
      ),
    );
  }
}
